﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.Portal.WebControls;
using RENIT.TagsCloud3D.Utils;
using System.Web.UI.WebControls;
using System.Web;

namespace RENIT.WebParts.TagsCloud3D.Controls
{
    public class TagsCloud3DView : UserControl
    {
        public TagCloudItem[] Tags { get; set; }

        public string TagsSourceParameters { get; set; }

        public bool UseOwnSWFObject { get; set; }

        public string SWFObjectUrl { get; set; }

        public bool UseTransparency { get; set; }

        public string CloudFlashUrl { get; set; }

        public string Width { get; set; }

        public string Height { get; set; }

        public string JSBodyLoadHandlerName
        {
            get { return ClientID + "_onBodyLoaded"; }
        }

        public TagsCloud3DView()
        {
            UseOwnSWFObject = Constants.UseOwnSWFObject;
            SWFObjectUrl = Constants.SWFObjectUrl;
            CloudFlashUrl = Constants.CloudFlashUrl;
            UseTransparency = Constants.TransparentByDefault;
            Width = Constants.Width;
            Height = Constants.Height;
            Tags = new TagCloudItem[0];
        }

        protected string GetTagsHtml(string symbolToEscape)
        {

            if (Tags == null || Tags.FirstOrDefault() == null)
            {
                return string.Empty;
            }
            StringBuilder buf = new StringBuilder();
            long peekCount = Tags.Max(tag => tag.Count);
            foreach (var tag in Tags)
            {
                buf.AppendFormat("<a href=\"{0}\" style=\"{1}\">{2}</a>", tag.Link, GetTagStyle(tag.Count, peekCount), tag.Name);
            }
            if (!string.IsNullOrEmpty(symbolToEscape))
            {
                foreach (char c in symbolToEscape)
                {
                    buf.Replace(string.Empty + c, string.Empty + '\\' + c);
                }
            }
            return buf.ToString();
        }

        protected virtual string GetTagStyle(long count, long peekCount)
        {
            return 1 + Math.Round(new decimal(3) * (new decimal(count) / new decimal(peekCount))).ToString();
        }
    }
}
